package com.dream.keigezhushou.Activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.dream.keigezhushou.Activity.acty.listen.HotRecommendActivity;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

/**
 * Created by mxmf-001 on 17/3/1.
 */
public class HotRecommendAdapter extends RecyclerView.Adapter<HotRecommendAdapter.MyViewHolder> {
    private  View headerView;

    private static final int HEAD = 1;
    public static final int NORMAL = 3;
    private Context context;
    private ArrayList<String>h;

    private  HotRecommendActivity.MyItemClickListener myItemClickListener;
    public HotRecommendAdapter(Context context, ArrayList<String> h) {

        this.context=context;
        this.h=h;

    }

    public void setOnItemClickListener(HotRecommendActivity.MyItemClickListener myItemClickListener) {
        this.myItemClickListener=myItemClickListener;
    }
    public void addHeaderView(View headerView) {
        this.headerView = headerView;
    }
    public int getHeaderViewCount() {
        return headerView == null ? 0 : 1;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD) {
            return new MyViewHolder(headerView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collected_music_list_item, null);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        if (getItemViewType(position) == HEAD) return ;
        holder.tvSinger.setText(h.get(position-getHeaderViewCount()));
        holder.iv_more01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (myItemClickListener != null) {
                    myItemClickListener.onBtnClick(position);
                }
            }
        });


        holder.Linearitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(myItemClickListener!=null){

                    myItemClickListener.onItemClick(position);

                }
            }
        });
        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"播放全部",Toast.LENGTH_SHORT).show();


            }
        });

    }



    @Override
    public int getItemCount() {
        return h.size()+getHeaderViewCount();
    }

    @Override
    public int getItemViewType(int position) {
        if(position<getHeaderViewCount() ) {
            return  HEAD;

        }else {
            return  NORMAL;
        }
    }



    public class MyViewHolder extends  RecyclerView.ViewHolder{
        TextView tvMusic;
        TextView tvSinger;
        ImageView iv_more01;
        LinearLayout Linearitem;

       public MyViewHolder(View itemView) {

           super(itemView);
           Linearitem=(LinearLayout)itemView.findViewById(R.id.item_music_list);
           iv_more01=(ImageView)itemView.findViewById(R.id.item_more);
           tvSinger=(TextView)itemView.findViewById(R.id.collected_music_singer);
           tvMusic=(TextView)itemView.findViewById(R.id.collected_music_name);

       }
   }
}
